package com.program.utils;

import com.program.eneity.Airplane;

/**
 * @program: AirportSystem
 * @description: 飞机队列
 * @author: 19071027
 * @create: 2021-09-07 16:01
 **/
public class PriorityQueue implements Queue {

    /**
     * 飞机队列
     */
    private Airplane[] priorityQueue;

    /**
     * 当前飞机数目
     */
    private int size;

    /**
     * 最大排队容量
     */
    private int MAX_SIZE = 4;

    public PriorityQueue() {
        size = 0;
        priorityQueue = new Airplane[MAX_SIZE];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        if (size == 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean add(Object o) {
        if (size != MAX_SIZE) {
            //队列未满
            priorityQueue[size] = (Airplane) o;
            size++;

            //根据燃油容量进行优先级排序 实现：冒泡排序
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size - i - 1; j++) {
                    if (priorityQueue[j + 1].getFuel() < priorityQueue[j].getFuel()) {
                        Airplane temp = priorityQueue[j];
                        priorityQueue[j] = priorityQueue[j + 1];
                        priorityQueue[j + 1] = temp;
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public Airplane remove() {
        if (size == 0) {
            return null;
        }
        Airplane airplane = priorityQueue[0];
        for (int i = 0; i < size - 1; i++) {
            priorityQueue[i] = priorityQueue[i + 1];
        }
        size--;
        return airplane;
    }

    @Override
    public Airplane peek() {
        return priorityQueue[0];
    }

    public boolean isFull() {
        return size == MAX_SIZE;
    }

    public boolean offer(Object o) {
        if (size != MAX_SIZE) {
            //队列未满
            priorityQueue[size] = (Airplane) o;
            size++;
            return true;
        }
        return false;
    }

}
